package com.excilys.blog.refactoring.refactored;

import java.util.ArrayList;
import java.util.List;

import com.excilys.blog.refactoring.GeoPoint;

class HullFinder {

    private final GeoPointSet geoPointSet;

    private final PathFinder  eastPathFinder;

    private final PathFinder  westPathFinder;

    private List<GeoPoint>    eastPath;
    private List<GeoPoint>    westPath;

    private GeoPoint          westernPoint;
    private GeoPoint          easternPoint;

    public HullFinder(GeoPointSet geoPointSet) {
        this.geoPointSet = geoPointSet;
        eastPathFinder = PathFinder.eastPathFinder(geoPointSet);
        westPathFinder = PathFinder.westPathFinder(geoPointSet);
    }

    public List<GeoPoint> findAntiClockwiseHull() {
        findBounds();
        findEastPath();
        findWestPath();
        return assembleHull();
    }

    private void findBounds() {
        Bounds bounds = geoPointSet.findBounds();
        westernPoint = bounds.getWesternPoint();
        easternPoint = bounds.getEasternPoint();
    }

    private void findEastPath() {
        eastPath = eastPathFinder.findPath(westernPoint, easternPoint);
    }

    private void findWestPath() {
        westPath = westPathFinder.findPath(easternPoint, westernPoint);
    }

    private List<GeoPoint> assembleHull() {
        List<GeoPoint> hull = new ArrayList<GeoPoint>();
        hull.addAll(eastPath);
        hull.addAll(westPath);
        hull.add(westernPoint);
        return hull;
    }

}
